package com.projectxxxxxxxxxx.screens;

import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Handler;
import android.provider.Settings;
import android.view.View;
import android.widget.RemoteViews;

import com.projectxxxxxxxxxx.Constants;
import com.projectxxxxxxxxxx.R;
import com.projectxxxxxxxxxx.toggle.Toggle;

public class ToogleScreen extends Screen {

	private static final boolean buttonOff = false;
	private static final boolean buttonOn = true;
	
    public ToogleScreen(final Context context, final int id) {
        super(context, id, "");

        // fill actionsView with views you need

        // WIFI actions
        actionsView.put(WifiManager.WIFI_STATE_CHANGED_ACTION, R.id.button_wifi);
        actionsView.put(ConnectivityManager.CONNECTIVITY_ACTION, R.id.button_wifi);
        actionsView.put(Constants.ACTION_TOGGLE_WIFI, R.id.button_on_wifi);
        actionsView.put(Constants.ACTION_TOGGLE_WIFI, R.id.button_off_wifi);

        // Phone state actions
        actionsView.put(AudioManager.RINGER_MODE_CHANGED_ACTION, R.id.button_phone_mode);
        actionsView.put(Constants.ACTION_TOGGLE_SILENT, R.id.button_off_phone_mode);
        actionsView.put(Constants.ACTION_TOGGLE_SILENT, R.id.button_on_phone_mode);
        actionsView.put(Constants.ACTION_TOGGLE_SILENT, R.id.button_middle_phone_mode);

        // Bluetooth actions
        actionsView.put(BluetoothAdapter.ACTION_STATE_CHANGED, R.id.button_bluetooth);
        actionsView.put(Constants.ACTION_TOGGLE_BLUETOOTH, R.id.button_on_bluetooth);
        actionsView.put(Constants.ACTION_TOGGLE_BLUETOOTH, R.id.button_off_bluetooth);

        // Flashlight actions
        actionsView.put(Constants.ACTION_TOGGLE_FLASH_LIGHT, R.id.button_flash_light);
        actionsView.put(Constants.ACTION_TOGGLE_FLASH_LIGHT, R.id.button_on_flash_light);
        actionsView.put(Constants.ACTION_TOGGLE_FLASH_LIGHT, R.id.button_off_flash_light);

        // Autorotate actions
        actionsView.put(Constants.ACTION_TOGGLE_AUTO_ROTATE, R.id.button_auto_rotate);
        actionsView.put(Constants.ACTION_TOGGLE_AUTO_ROTATE, R.id.button_on_auto_rotate);
        actionsView.put(Constants.ACTION_TOGGLE_AUTO_ROTATE, R.id.button_off_auto_rotate);

        context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
                false, toggleStateChangedObserver);

        // Data actions
        actionsView.put(Constants.ACTION_ANY_DATA_STATE, R.id.button_data);
        actionsView.put(Constants.ACTION_TOGGLE_DATA, R.id.button_on_data);
        actionsView.put(Constants.ACTION_TOGGLE_DATA, R.id.button_off_data);

        // Mobile actions
        actionsView.put(Constants.ACTION_ANY_DATA_STATE, R.id.button_mobile);
        actionsView.put(Constants.ACTION_TOGGLE_MOBILE, R.id.button_on_mobile);
        actionsView.put(Constants.ACTION_TOGGLE_MOBILE, R.id.button_off_mobile);
    }

    private final ContentObserver toggleStateChangedObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);

            if (!selfChange) {
                views.setViewVisibility(R.id.button_off_auto_rotate, View.GONE);
                views.setViewVisibility(R.id.button_on_auto_rotate_off, View.VISIBLE);
            } else {
                views.setViewVisibility(R.id.button_on_auto_rotate, View.GONE);
                views.setViewVisibility(R.id.button_off_auto_rotate, View.VISIBLE);
            }

            setAllButtonsListeners(views);

            updateWidget(views);
        }
    };

    @Override
    public void updateAllView(final RemoteViews views) {
        final Intent intent = new Intent(Constants.ACTION_UPDATE_TOOGLE_ALL);
        context.sendBroadcast(intent);
        
        setAllButtonsListeners(views);
    }

    @Override
    public void updateView(Intent intent, final String action, final RemoteViews views) {
    	
    	 boolean ind = false;
    	
    	if(Constants.ACTION_UPDATE_TOOGLE_ALL.equals(action)){
    		checkAllToogles(views);
    		return;
    	}

    	if(actionsView == null){
    		return;
    	}
    	
    	switch(actionsView.get(action)){
    		case R.id.button_off_wifi:
    		case R.id.button_on_wifi:
    			Toggle.WIFI.toggle();
                views.setViewVisibility(R.id.button_off_wifi, View.GONE);
                views.setViewVisibility(R.id.button_on_wifi, View.GONE);
                views.setViewVisibility(R.id.button_middle_wifi, View.VISIBLE);
    			return;

            case R.id.button_off_phone_mode:
            case R.id.button_on_phone_mode:
            case R.id.button_middle_phone_mode:
                Toggle.SILENT.toggle();
                return;

            case R.id.button_off_bluetooth:
            case R.id.button_on_bluetooth:
                Toggle.BLUETOOTH.toggle();
                return;

            case R.id.button_off_flash_light:
            case R.id.button_on_flash_light:
                Toggle.FLASH_LIGHT.toggle();
                return;

            case R.id.button_off_auto_rotate:
            case R.id.button_on_auto_rotate:
                Toggle.AUTO_ROTATE.toggle();
                return;

            case R.id.button_off_data:
            case R.id.button_on_data:
                Toggle.DATA.toggle();
                return;

            case R.id.button_off_mobile:
            case R.id.button_on_mobile:
                Toggle.MOBILE.toggle();
                return;
    	}
    	
    	switch(actionsView.get(action)){

	    	case R.id.button_wifi:
	    		ind = Toggle.WIFI.get();
	            if (ind) {
	                views.setViewVisibility(R.id.button_off_wifi, View.GONE);
	                views.setViewVisibility(R.id.button_on_wifi, View.VISIBLE);
                    views.setViewVisibility(R.id.button_middle_wifi, View.GONE);
	            } else {
	                views.setViewVisibility(R.id.button_on_wifi, View.GONE);
	                views.setViewVisibility(R.id.button_off_wifi, View.VISIBLE);
                    views.setViewVisibility(R.id.button_middle_wifi, View.GONE);
	            }
	    		break;

            case R.id.button_phone_mode:
                ind = Toggle.SILENT.get();
                if (ind) {
                    views.setViewVisibility(R.id.button_off_phone_mode, View.GONE);
                    views.setViewVisibility(R.id.button_on_phone_mode, View.VISIBLE);
                    views.setViewVisibility(R.id.button_middle_phone_mode, View.GONE);
                } else {
                    ind = Toggle.VIBRATE.get();
                    if (ind) {
                        views.setViewVisibility(R.id.button_on_phone_mode, View.GONE);
                        views.setViewVisibility(R.id.button_off_phone_mode, View.GONE);
                        views.setViewVisibility(R.id.button_middle_phone_mode, View.VISIBLE);
                    } else {
                        views.setViewVisibility(R.id.button_on_phone_mode, View.GONE);
                        views.setViewVisibility(R.id.button_off_phone_mode, View.VISIBLE);
                        views.setViewVisibility(R.id.button_middle_phone_mode, View.GONE);
                    }
                }
                break;

            case R.id.button_bluetooth:
                ind = Toggle.BLUETOOTH.get();
                if (ind) {
                    views.setViewVisibility(R.id.button_off_bluetooth, View.GONE);
                    views.setViewVisibility(R.id.button_on_bluetooth, View.VISIBLE);
                } else {
                    views.setViewVisibility(R.id.button_on_bluetooth, View.GONE);
                    views.setViewVisibility(R.id.button_off_bluetooth, View.VISIBLE);
                }
                break;

            case R.id.button_flash_light:
                ind = Toggle.FLASH_LIGHT.get();
                if (ind) {
                    views.setViewVisibility(R.id.button_off_flash_light, View.GONE);
                    views.setViewVisibility(R.id.button_on_flash_light, View.VISIBLE);
                } else {
                    views.setViewVisibility(R.id.button_on_flash_light, View.GONE);
                    views.setViewVisibility(R.id.button_off_flash_light, View.VISIBLE);
                }
                break;

            case R.id.button_auto_rotate:
            	ind = Toggle.AUTO_ROTATE.get();
                if (ind) {
                    views.setViewVisibility(R.id.button_off_auto_rotate, View.GONE);
                    views.setViewVisibility(R.id.button_on_auto_rotate, View.VISIBLE);
                } else {
                    views.setViewVisibility(R.id.button_on_auto_rotate, View.GONE);
                    views.setViewVisibility(R.id.button_off_auto_rotate, View.VISIBLE);
                }
            	break;
                
            case R.id.button_data:

            case R.id.button_mobile:
                break;

    	}
    	
    	
    	setAllButtonsListeners(views);

        updateWidget(views);
    }
    
    private void setAllButtonsListeners(final RemoteViews views){
    	Intent intentWifi = new Intent(Constants.ACTION_TOGGLE_WIFI);
        PendingIntent pendingIntentWifi = PendingIntent.getBroadcast(context, 0, intentWifi, 0);
        views.setOnClickPendingIntent(R.id.button_wifi, pendingIntentWifi);
        views.setOnClickPendingIntent(R.id.button_middle_wifi_on, pendingIntentWifi);
        views.setOnClickPendingIntent(R.id.button_middle_wifi_off, pendingIntentWifi);
        views.setOnClickPendingIntent(R.id.button_on_wifi_on, pendingIntentWifi);
        views.setOnClickPendingIntent(R.id.button_on_wifi_off, pendingIntentWifi);
        views.setOnClickPendingIntent(R.id.button_off_wifi_on, pendingIntentWifi);
        views.setOnClickPendingIntent(R.id.button_off_wifi_off, pendingIntentWifi);

        Intent intentSilent = new Intent(Constants.ACTION_TOGGLE_SILENT);
        PendingIntent pendingIntentSilent = PendingIntent.getBroadcast(context, 0, intentSilent, 0);
        views.setOnClickPendingIntent(R.id.button_phone_mode, pendingIntentSilent);
        views.setOnClickPendingIntent(R.id.button_on_phone_mode_on, pendingIntentSilent);
        views.setOnClickPendingIntent(R.id.button_on_phone_mode_off, pendingIntentSilent);
        views.setOnClickPendingIntent(R.id.button_middle_phone_mode_on, pendingIntentSilent);
        views.setOnClickPendingIntent(R.id.button_middle_phone_mode_off, pendingIntentSilent);
        views.setOnClickPendingIntent(R.id.button_off_phone_mode_on, pendingIntentSilent);
        views.setOnClickPendingIntent(R.id.button_off_phone_mode_off, pendingIntentSilent);

        Intent intentBluetooth = new Intent(Constants.ACTION_TOGGLE_BLUETOOTH);
        PendingIntent pendingIntentBluetooth = PendingIntent.getBroadcast(context, 0, intentBluetooth, 0);
        views.setOnClickPendingIntent(R.id.button_bluetooth, pendingIntentBluetooth);
        views.setOnClickPendingIntent(R.id.button_on_bluetooth_on, pendingIntentBluetooth);
        views.setOnClickPendingIntent(R.id.button_on_bluetooth_off, pendingIntentBluetooth);
        views.setOnClickPendingIntent(R.id.button_off_bluetooth_on, pendingIntentBluetooth);
        views.setOnClickPendingIntent(R.id.button_off_bluetooth_off, pendingIntentBluetooth);

        Intent intentFlash = new Intent(Constants.ACTION_TOGGLE_FLASH_LIGHT);
        PendingIntent pendingIntentFlash = PendingIntent.getBroadcast(context, 0, intentFlash, 0);
        views.setOnClickPendingIntent(R.id.button_flash_light, pendingIntentFlash);
        views.setOnClickPendingIntent(R.id.button_on_flash_light_on, pendingIntentFlash);
        views.setOnClickPendingIntent(R.id.button_on_flash_light_off, pendingIntentFlash);
        views.setOnClickPendingIntent(R.id.button_off_flash_light_on, pendingIntentFlash);
        views.setOnClickPendingIntent(R.id.button_off_flash_light_off, pendingIntentFlash);

        Intent intentRotate = new Intent(Constants.ACTION_TOGGLE_AUTO_ROTATE);
        PendingIntent pendingIntentRotate = PendingIntent.getBroadcast(context, 0, intentRotate, 0);
        views.setOnClickPendingIntent(R.id.button_auto_rotate, pendingIntentRotate);
        views.setOnClickPendingIntent(R.id.button_on_auto_rotate_on, pendingIntentRotate);
        views.setOnClickPendingIntent(R.id.button_on_auto_rotate_off, pendingIntentRotate);
        views.setOnClickPendingIntent(R.id.button_off_auto_rotate_on, pendingIntentRotate);
        views.setOnClickPendingIntent(R.id.button_off_auto_rotate_off, pendingIntentRotate);

        Intent intentData = new Intent(Constants.ACTION_TOGGLE_DATA);
        PendingIntent pendingIntentData = PendingIntent.getBroadcast(context, 0, intentData, 0);
        views.setOnClickPendingIntent(R.id.button_data, pendingIntentData);
        views.setOnClickPendingIntent(R.id.button_on_data_on, pendingIntentData);
        views.setOnClickPendingIntent(R.id.button_on_data_off, pendingIntentData);
        views.setOnClickPendingIntent(R.id.button_off_data_on, pendingIntentData);
        views.setOnClickPendingIntent(R.id.button_off_data_off, pendingIntentData);

        Intent intentMobile = new Intent(Constants.ACTION_TOGGLE_MOBILE);
        PendingIntent pendingIntentMobile = PendingIntent.getBroadcast(context, 0, intentMobile, 0);
        views.setOnClickPendingIntent(R.id.button_mobile, pendingIntentMobile);
        views.setOnClickPendingIntent(R.id.button_on_mobile_on, pendingIntentMobile);
        views.setOnClickPendingIntent(R.id.button_on_mobile_off, pendingIntentMobile);
        views.setOnClickPendingIntent(R.id.button_off_mobile_on, pendingIntentMobile);
        views.setOnClickPendingIntent(R.id.button_off_mobile_off, pendingIntentMobile);

    }

    private void checkAllToogles(final RemoteViews views){
        boolean ind = Toggle.AUTO_ROTATE.get();
        if (ind) {
            views.setViewVisibility(R.id.button_off_auto_rotate, View.GONE);
            views.setViewVisibility(R.id.button_on_auto_rotate, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.button_on_auto_rotate, View.GONE);
            views.setViewVisibility(R.id.button_off_auto_rotate, View.VISIBLE);
        }

        ind = Toggle.BLUETOOTH.get();
        if (ind) {
            views.setViewVisibility(R.id.button_off_bluetooth, View.GONE);
            views.setViewVisibility(R.id.button_on_bluetooth, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.button_on_bluetooth, View.GONE);
            views.setViewVisibility(R.id.button_off_bluetooth, View.VISIBLE);
        }

        ind = Toggle.WIFI.get();
        if (ind) {
            views.setViewVisibility(R.id.button_off_wifi, View.GONE);
            views.setViewVisibility(R.id.button_on_wifi, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.button_on_wifi, View.GONE);
            views.setViewVisibility(R.id.button_off_wifi, View.VISIBLE);
        }

        ind = Toggle.DATA.get();
        if (ind) {
            views.setViewVisibility(R.id.button_off_data, View.GONE);
            views.setViewVisibility(R.id.button_on_data, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.button_on_data, View.GONE);
            views.setViewVisibility(R.id.button_off_data, View.VISIBLE);
        }

        ind = Toggle.FLASH_LIGHT.get();
        if (ind) {
            views.setViewVisibility(R.id.button_off_flash_light, View.GONE);
            views.setViewVisibility(R.id.button_on_flash_light, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.button_on_flash_light, View.GONE);
            views.setViewVisibility(R.id.button_off_flash_light, View.VISIBLE);
        }

        ind = Toggle.MOBILE.get();
        if (ind) {
            views.setViewVisibility(R.id.button_off_mobile, View.GONE);
            views.setViewVisibility(R.id.button_on_mobile, View.VISIBLE);
        } else {
            views.setViewVisibility(R.id.button_on_mobile, View.GONE);
            views.setViewVisibility(R.id.button_off_mobile, View.VISIBLE);
        }

        ind = Toggle.SILENT.get();
        if (ind) {
            views.setViewVisibility(R.id.button_off_phone_mode, View.GONE);
            views.setViewVisibility(R.id.button_on_phone_mode, View.VISIBLE);
            views.setViewVisibility(R.id.button_middle_phone_mode, View.GONE);
        } else {
            ind = Toggle.VIBRATE.get();
            if (ind) {
                views.setViewVisibility(R.id.button_on_phone_mode, View.GONE);
                views.setViewVisibility(R.id.button_off_phone_mode, View.GONE);
                views.setViewVisibility(R.id.button_middle_phone_mode, View.VISIBLE);
            } else {
                views.setViewVisibility(R.id.button_on_phone_mode, View.GONE);
                views.setViewVisibility(R.id.button_off_phone_mode, View.VISIBLE);
                views.setViewVisibility(R.id.button_middle_phone_mode, View.GONE);
            }
        }
    }
    
    public class LoadEnums extends AsyncTask<String, Void, Boolean> {

        final Context context;
        final RemoteViews views;
        String[] args; // contains actions

        public LoadEnums(final Context context, final RemoteViews views) {
            this.views = views;
            this.context = context;
        }

        @Override
        protected Boolean doInBackground(final String... args) {
            this.args = args;
            // use enums here
            return null;
        }

        @Override
        protected void onPostExecute(final Boolean result) {
            // update views with getViewAction(string action)
            // getViewAction - return views id to need update here

        }

    }

}
